#include <stdlib.h>
int findUnsortedSubarray(int* nums, int numsSize){
 /* int begin=numsSize-1;
  int last=0,j=0;
    int *stk=(int *)malloc(sizeof(int)*numsSize);
    int cnt=0,top=0;
    for(int i=0;i<numsSize;i++){
      int k=i;
      if(top!=0){
        while(nums[k]<nums[stk[top-1]]){
          last=i;
          int t=nums[k];
          nums[k]=nums[stk[top-1]];
          nums[stk[top-1]]=t;
          top--;
          k--;
          begin=top<begin?top-1:begin;
          if(top==0) break;
        }
      }
      stk[top]=i;
      top++;
    }
    if(begin==numsSize-1){
      begin=0;
    }
    cnt=last-begin;
    return cnt;*/
 int cmp(int* a, int* b) {
    return *a - *b;
}
    int arr[numsSize];
   memcpy(arr, nums, sizeof(int) * numsSize);
  /*  for(int i=0;i<numsSize;i++){
       arr[i]=nums[i];
    }*/
   qsort(arr,numsSize,sizeof(int),cmp);
    int i=0,j=numsSize-1;
    while(nums[i]==arr[i]&&i<j) i++;
    while(nums[j]==arr[j]&&j>i) j--;
    if(i==numsSize-1) return 0;
    return j-i+1;
}
